﻿using SMS.cntr;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMS.util;
using System.Resources;

namespace SMS.view
{
    public partial class ReportAllCoursesForm : Form
    {
        public ReportAllCoursesForm()
        {
            InitializeComponent();
        }
        
        private void bindCourse()
        {
            cbCourse.DataSource = GradeCntr.Instance.listCourse();
            cbCourse.DisplayMember = "cor_name";
            cbCourse.ValueMember = "cor_id";
            cbCourse.Text = "Chọn Môn Học";
        }

        private void bindSemester()
        {
            cbSemester.DataSource = GradeCntr.Instance.listSemester();
            cbSemester.DisplayMember = "sem_name";
            cbSemester.ValueMember = "sem_id";
            cbSemester.Text = "Chọn Học Kỳ";
            cbSemester.Enabled = false;
        }

        private void cbCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                bindGridViewListByCourse();
                cbSemester.Enabled = true;
            }
            catch (Exception ex) { }
        }

        private void cbSemester_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                bindGridViewListALL();
            }
            catch (Exception ex) { }
        }

        private void frmReportCourse_Load(object sender, EventArgs e)
        {
            bindCourse();
            bindSemester();
            changeLang(I18nUtil.vnLang);
        }

        private void bindGridViewListALL()
        {
            gridViewCourseReport.Rows.Clear();
            int corID = int.Parse(cbCourse.SelectedValue.ToString());
            int semID = int.Parse(cbSemester.SelectedValue.ToString());
            DataTable table = GradeCntr.Instance.listGradeRate(corID, semID);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                gridViewCourseReport.Rows.Add(i + 1, table.Rows[i]["cla_name"].ToString(), table.Rows[i]["cla_counted"].ToString(), table.Rows[i]["cla_passed"].ToString(), table.Rows[i]["rate"].ToString());
                gridViewCourseReport.Rows[gridViewCourseReport.RowCount - 1].Tag = table.Rows[i];
            }
            gridViewCourseReport.ClearSelection();
        }

        private void bindGridViewListByCourse()
        {
            gridViewCourseReport.Rows.Clear();
            int corID = int.Parse(cbCourse.SelectedValue.ToString());
            DataTable table = GradeCntr.Instance.listGradeRateByCourse(corID);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                gridViewCourseReport.Rows.Add(i + 1, table.Rows[i]["cla_name"].ToString(), table.Rows[i]["cla_counted"].ToString());
                gridViewCourseReport.Rows[gridViewCourseReport.RowCount - 1].Tag = table.Rows[i];
            }
            gridViewCourseReport.ClearSelection();
        }

        public void changeLang(string lang)
        {
            I18nUtil.init(lang, new ResourceManager(I18nUtil.resourcePath, typeof(ReportAllCoursesForm).Assembly));

            this.Text = I18nUtil.getString("reportAllCourse");
            lblCourse.Text = I18nUtil.getString("lblCourse");
            lblSemester.Text = I18nUtil.getString("lblSemester");
            gridViewCourseReport.Columns[0].HeaderText = I18nUtil.getString("no");
            gridViewCourseReport.Columns[1].HeaderText = I18nUtil.getString("lblClass");
            gridViewCourseReport.Columns[2].HeaderText = I18nUtil.getString("lblClassCapacity");
            gridViewCourseReport.Columns[3].HeaderText = I18nUtil.getString("numberOfPassStudent");
            gridViewCourseReport.Columns[4].HeaderText = I18nUtil.getString("rateOfPass");
        }
    }
}
